package trading.ctm.yunjing.module.main.activity

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.support.v4.app.FragmentTabHost
import android.support.v4.content.LocalBroadcastManager
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import cc.vv.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_home.*
import trading.ctm.yunjing.R
import trading.ctm.yunjing.module.auction.fragment.AuctionFragment
import trading.ctm.yunjing.module.information.fragment.InformationFragment
import trading.ctm.yunjing.module.main.fragment.HomeFragment
import trading.ctm.yunjing.module.mine.fragment.MineFragment

/**
 * @desc 首页
 * @author zouxiaorui
 * @date  2018/8/8
 */
class HomeActivity : BaseActivity() {

    var lm: LocalBroadcastManager? = null

    val myReceiver = MyBroadCast()
    var tabHost: FragmentTabHost? = null

    override fun initLayoutRes(): Int {
        return R.layout.activity_home
    }


    override fun initView(bundle: Bundle?) {
        super.initView(bundle)

        lm = LocalBroadcastManager.getInstance(this@HomeActivity.applicationContext)
        val tabFonts = resources.getStringArray(R.array.table_name)
        val tabImages = intArrayOf(R.drawable.selector_home, R.drawable.selector_information, R.drawable.selector_auction, R.drawable.selector_mine)
        val fragments = listOf(HomeFragment::class.java,
                InformationFragment::class.java,
                AuctionFragment::class.java,
                MineFragment::class.java)
        fth_tabhost.setup(this, supportFragmentManager, R.id.fl_tab_content)
        for (i in 0 until fragments.size) {
            val tpc = fth_tabhost.newTabSpec(tabFonts[i]).setIndicator(initTableView(i, tabFonts, tabImages))
            fth_tabhost.addTab(tpc, fragments[i], null)
        }
        tabHost = fth_tabhost
    }

    override fun initData(data: Bundle?) {
        super.initData(data)

    }

    override fun initAction(data: Bundle?) {
        super.initAction(data)
        val filter = IntentFilter()
        filter.addAction("fragment")
        lm!!.registerReceiver(myReceiver, filter)
    }

    override fun onDestroy() {
        super.onDestroy()
        lm!!.unregisterReceiver(myReceiver)
    }

    /**
     * 初始化FragmentTabHost
     */
    private fun initTableView(i: Int, tab_fonts: Array<String>, tab_images: IntArray): View {
        val view = View.inflate(this@HomeActivity, R.layout.tab_view, null)
        val tabImg: ImageView = view.findViewById(R.id.iv_tav_img)
        val tabFont: TextView = view.findViewById(R.id.tv_tav_font)
        tabImg.setImageResource(tab_images[i])
        tabFont.text = tab_fonts[i]
        return view
    }

    inner class MyBroadCast : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            val action: String = intent!!.action
            if (action == "fragment") {
                val i = intent.getIntExtra("fragment_key", 0)
                tabHost!!.currentTab = i
                if(i == 0){
                    tabHost!!.visibility = View.VISIBLE
                }else{
                    tabHost!!.visibility = View.GONE
                }
            }
        }

    }

}
